﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using Microsoft.Win32;
using System.IO;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace PcBackgroundMgr.Controls
{
    /// <summary>
    /// ImageSelector.xaml 的交互逻辑
    /// </summary>
    public partial class ImageSelector : ImageSelectorBase
    {
        public ImageSelector()
        {
            InitializeComponent();
        }

        protected override void OnClick()
        {
            string path = FilePath;
            base.OnClick();
            if (path != FilePath && SelectType == SelectButtonBase.SelectTypeEnum.Pictrue)
            {
                FileName = Path.GetFileName(FilePath);//获取文件名

                BinaryReader binReader = new BinaryReader(File.Open(FilePath, FileMode.Open));
                FileInfo fileInfo = new FileInfo(FilePath);
                byte[] bytes = binReader.ReadBytes((int)fileInfo.Length);
                binReader.Close();

                // Init bitmap
                BitmapImage bitmap = new BitmapImage();
                bitmap.BeginInit();
                bitmap.StreamSource = new MemoryStream(bytes);
                bitmap.EndInit();

                Source = bitmap;
                //Source = new BitmapImage(new Uri(FilePath));
            }
        }
        
    }
    [System.ComponentModel.DesignTimeVisible(false)]
    public partial class ImageSelectorBase : SelectButtonBase
    {
        public ImageSource Source
        {
            get { return (ImageSource)GetValue(SourceProperty); }
            set { SetValue(SourceProperty, value); }
        }

        // Using a DependencyProperty as the backing store for MyProperty.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty SourceProperty =
            DependencyProperty.Register("Source", typeof(ImageSource),
            typeof(ImageSelectorBase), new UIPropertyMetadata(null));

        
    }

}
